package com.imedia.frame.lucene.service.impl;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.Term;

import com.imedia.frame.lucene.model.IndexModel;
import com.imedia.frame.lucene.service.AbstractIndexService;
import com.imedia.frame.lucene.service.IIndexService;
import com.imedia.frame.lucene.utils.HTMLDataUtils;
import com.imedia.frame.lucene.utils.IndexUtils;

/**
 * Title: 全站检索
 * Description: lucene检索模块类</p>
 * <p>Copyright: Copyright (c) 2009 </p>
 * <p>Company: </p>
 * @author qiuy
 * @version 1.0
 */
public  class IndexServiceImpl extends AbstractIndexService implements IIndexService {
	
	public void initIndexDir() {
		initWrite(true);
		SEOIndexWrite(allCount);
		Document document = null;
		document = new Document();
		Field fieldId = null;
		Field fieldTitle = null;
		Field fieldAuthor = null;
		Field fieldContent = null;
		Field fieldUrl = null;
		Field fieldType = null;
		fieldId = new Field("id", WHITE_FLAG,Field.Store.YES, Field.Index.NO);
		fieldTitle = new Field("title", WHITE_FLAG,Field.Store.YES, Field.Index.TOKENIZED);
		fieldAuthor = new Field("author", WHITE_FLAG,Field.Store.YES, Field.Index.TOKENIZED);
		fieldContent = new Field("content", WHITE_FLAG,Field.Store.YES, Field.Index.TOKENIZED);
		fieldUrl = new Field("url", WHITE_FLAG,Field.Store.YES, Field.Index.NO);
		fieldType = new Field("type", WHITE_FLAG,Field.Store.YES, Field.Index.NO);

		document.add(fieldId);
		document.add(fieldTitle);
		document.add(fieldAuthor);
		document.add(fieldContent);
		document.add(fieldUrl);
		document.add(fieldType);
		try {
			writer.optimize();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		Logger.getRootLogger().info(">>>>>>>>>>>>>>>>>>>>>初始化索引目录成功");
	}
	
	@SuppressWarnings("unchecked")
	public void createIndex(List indexList) throws Exception {
		try {
			initWrite(false);
			SEOIndexWrite(allCount);
			Document document = null;
			Field fieldId = null;
			Field fieldTitle = null;
			Field fieldAuthor = null;
			Field fieldContent = null;
			Field fieldUrl = null;
			Field fieldType = null;
			for (Iterator iterator = indexList.iterator(); iterator.hasNext();) {
				Object object = iterator.next();
				IndexModel indexModel = null;
				if (object instanceof IndexModel) {
					document = new Document();
					indexModel = (IndexModel) object;
					String url = IndexUtils.getString(indexModel.getUrl());
					if (!"".equals(url)) {
						String content = HTMLDataUtils.getReallyString(IndexUtils.getString(indexModel.getContent()),"P");
						fieldId = new Field("id", IndexUtils.getString(indexModel.getId()),Field.Store.YES, Field.Index.NO);
						fieldTitle = new Field("title", IndexUtils.getString(indexModel.getTitle()),Field.Store.YES, Field.Index.TOKENIZED);
						fieldAuthor = new Field("author", IndexUtils.getString(indexModel.getAuthor()),Field.Store.YES, Field.Index.TOKENIZED);
						fieldContent = new Field("content", content,Field.Store.YES, Field.Index.TOKENIZED);
						fieldUrl = new Field("url", IndexUtils.getString(indexModel.getUrl()),Field.Store.YES, Field.Index.NO);
						fieldType = new Field("type", IndexUtils.getString(indexModel.getType()),Field.Store.YES, Field.Index.NO);

						document.add(fieldId);
						document.add(fieldTitle);
						document.add(fieldAuthor);
						document.add(fieldContent);
						document.add(fieldUrl);
						document.add(fieldType);
						try {
							writer.addDocument(document);
						} catch (Exception e) {
							Logger.getRootLogger().info(">>>>>>>>>>>>>>>>>>>>>创建索引异常");
						}
					}
				}
			}
			try {
				writer.optimize(); 
				writer.close();
				Logger.getRootLogger().info(">>>>>>>>>>>>>>>>>>>>>创建索引成功");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			Logger.getRootLogger().info("创建索引异常------------------->Exception");
			try {
				writer.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			throw e;
		}
	}
	public void deleteIndex(Object object) {
		initWrite(false);
		if (object == null) {
			return;
		}
		String str = "";
		if (object instanceof String) {
			str =  object.toString();
		}
		Term term = new Term("id",str);
		try {
			writer.deleteDocuments(term); 
			writer.optimize();
			writer.close();
		} catch (Exception e) {
			Logger.getRootLogger().info("删除索引异常------------------->Exception");
			e.printStackTrace();
		}
		
	}
	public void updateIndex(Object object) {
		initWrite(false);
		Document document = null;
		Field fieldId = null;
		Field fieldTitle = null;
		Field fieldAuthor = null;
		Field fieldContent = null;
		Field fieldUrl = null;
		Field fieldType = null;
		IndexModel indexModel = null;
		if (object instanceof IndexModel) {
			indexModel = (IndexModel) object;
		}
		Term term = new Term("id",indexModel.getId());
		try {
			writer.deleteDocuments(term);//删除现有索引
			document = new Document();
			String content = HTMLDataUtils.getReallyString(IndexUtils.getString(indexModel.getContent()), "P");
			fieldId = new Field("id", IndexUtils.getString(indexModel.getId()),Field.Store.YES, Field.Index.NO);
			fieldTitle = new Field("title", IndexUtils.getString(indexModel.getTitle()),Field.Store.YES, Field.Index.TOKENIZED);
			fieldAuthor = new Field("author", IndexUtils.getString(indexModel.getAuthor()),Field.Store.YES, Field.Index.TOKENIZED);
			fieldContent = new Field("content", content,Field.Store.YES, Field.Index.TOKENIZED);
			fieldUrl = new Field("url", IndexUtils.getString(indexModel.getUrl()),Field.Store.YES, Field.Index.NO);
			fieldType = new Field("type", IndexUtils.getString(indexModel.getType()),Field.Store.YES, Field.Index.NO);
			
			document.add(fieldId);
			document.add(fieldTitle);
			document.add(fieldAuthor);
			document.add(fieldContent);
			document.add(fieldUrl);
			document.add(fieldType);
			
			writer.addDocument(document);
			writer.optimize();
			writer.close();
		} catch (Exception e) {
			Logger.getRootLogger().info("删除索引异常------------------->Exception");
			e.printStackTrace();
		}
	}
	
	/**
	 * <p>
	 * Description: 初始化write
	 * </p>
	 * 
	 * @param isNew
	 *            true 重新建立索引 false 追加索引
	 */
	protected  void initWrite(boolean isNew){
		initMMAnalyzerIndexWrite(isNew);
	}
}
